/*******************************************************************************

PooledRegions analysis_dist_AV.do

2022.10.02 (DJ) extracted from <PooledRegions analysis_FARMER_robust.do>

*******************************************************************************/


/****************************************************************************************************************************************************************
	CDF of distance travelled to agrovet
****************************************************************************************************************************************************************/
	use "${analysis}/Manyara Farmer_mktaccess.dta", clear
		drop secf*
		rename (survey_district survey_ward survey_village) (district ward village_name)
		
		// add placeholders for the variables that have not been calculated. 
		// This makes it easier for excel formatting of the regression tables.
		gen buy_only_not_sell15 = .
		gen sell_only_not_buy15 = .
		
		gen buy_only_not_sell = buy_only_not_sell17
		gen sell_only_not_buy = sell_only_not_buy17
		
		//keep survey_region district ward village_name ${sampweight} google_vil_* google_farmer* ///
		//	 `farmer_varlist' `farmer_controls' `farmer_missing' `faocontrol'
		
		tempfile manyarafarmer
		save `manyarafarmer'
		
	
	use "${analysis}/Kilimanjaro Farmer_mktaccess.dta", clear
		// bring in the sampling weight
		merge m:1 village_name ward district using "${analysis}/Kilimanjaro Census.dta"
		keep if _merge==3
		rename census_numhh_pervillage numhh_pervillage
		
		gen buy_only_not_sell17 = .
		gen sell_only_not_buy17 = .
		
		//keep survey_region district ward village_name ${sampweight} google_vil_* google_farmer* ///
		//	 `farmer_varlist' `farmer_controls' `farmer_missing' `faocontrol'
			 
		append using `manyarafarmer'
		
		
		sum google_farmer_AVvil_km
		
		
// generate CDFs		
	local title1 Kilometers to agrovet
	local filename1 "CDF_km_travelled_to_agrovet"
	
	local i 0
	
	cap graph drop _all
	foreach var in google_farmer_AVvil_km {
				   
		local ++i
		cap drop var`i'_cdf
		cumul `var', gen(var`i'_cdf)
		sum `var', d
		
		#d;
		scatter var`i'_cdf `var', sort ytitle("% at or below", size(medlarge)) 
		xtick(0 (5) 150) xlabel(0 (10) 150)
		xtitle("`title`i''", size(medlarge)) xline(`r(p50)', lp(-)) xline(`r(p90)', lp(-))
		graphregion(color(white)) msize(vsmall) name(gcdf`i')
		;
		#d cr
		graph export "${pool_results}/`filename`i''_cdf.pdf", replace
		
	}

exit
